

#ifndef EM_BLE_H__
#define EM_BLE_H__

#include <stdint.h>
#include <stdbool.h>
#include "gap_conn_le.h"
#include "tts.h"
#include "wechats.h"
#include "ancs.h"

/*
    RTL876x define
*/
extern T_SERVER_ID wechats_srv_id;
extern T_SERVER_ID tts_srv_id; 
//////////////////////////////////////////////////////////////


#define    EM_BLE_HID_MEDIA_CTL_INVALID        0x00
#define    EM_BLE_HID_MEDIA_CTL_PLAY_PAUSE     0x01
#define    EM_BLE_HID_MEDIA_CTL_AL_CONTROL     0x02
#define    EM_BLE_HID_MEDIA_CTL_NEXT_TRACK     0x04
#define    EM_BLE_HID_MEDIA_CTL_PREVIOUS_TRACK 0x08
#define    EM_BLE_HID_MEDIA_CTL_VOLUME_DOWN    0x10
#define    EM_BLE_HID_MEDIA_CTL_VOLUME_UP      0x20
#define    EM_BLE_HID_MEDIA_CTL_AC_FORWARD     0x40
#define    EM_BLE_HID_MEDIA_CTL_AC_BACK        0x80


#define em_ble_ancs_action_handle   ancs_perform_calll_action

#define EM_BLE_LITE_DATA_NOTIF_HANDLE(CONN, DATA, SIZE)         tts_send_val_data( CONN, tts_srv_id, DATA, SIZE)
#define EM_BLE_PUS_DATA_NOTIF_HANDLE(CONN, DATA, SIZE)          tts_send_tx_data( CONN, tts_srv_id, DATA, SIZE )
#define EM_BLE_WECHATS_DATA_NOTIFY_HANDLE(CONN, DATA, SIZE)     wechats_send_pedo_data(CONN, wechats_srv_id, DATA, SIZE)

#define GAP_CONN_HANDLE_INVALID     0xFFFF

typedef struct
{
    uint8_t localMAC[6];
    uint8_t localMACR[6]; // reverse of MAC
    
    uint16_t conn_handle;   // 
    uint16_t finalMTU; 
    
//    bool connected;         // indicate connection
    bool ready;             // BLE stack is ready 
    bool enable;            // enable BLE to work 
    bool latency_disable;   // disable latency 
    bool host_is_iOS;       // connect to iOS host 
}em_ble_desc_t;

extern em_ble_desc_t   em_ble_desc; 

#if LOCATION_ADVERTISING_SUPPORT
typedef struct {
    uint8_t index;
    uint8_t data;
    float freq;
}location_interval_st;

typedef struct {
    uint8_t index;
    uint8_t reg;
    int8_t  dbm;
}location_adv_tx_power_st;

typedef struct sos_struct
{
    uint8_t flag;
    uint8_t count;
    uint8_t flag30;
    uint8_t count30;
}sos_st;

typedef struct {
    uint8_t state;
    uint8_t wakeup;
    sos_st  sos;
}location_adv_info_st;

extern const location_interval_st location_interval[];
#endif

void em_ble_init(void);

void em_ble_on_connect(uint16_t conn_handle);

void em_ble_on_disconnect(void);

void em_ble_start(void); 

void em_ble_stop(void);

void em_ble_on_stack_ready(void);

void em_ble_on_advertising_success(void);

void em_ble_advertising_start(void);

void em_ble_latency_disable(uint16_t conn_handle );

void em_ble_latency_enable(uint16_t conn_handle);

void em_ble_latency_check_timeout(void);

bool em_ble_media_set(uint16_t conn_handle, uint8_t cmd);

void em_ble_advertise_data_update_with_battery(uint8_t data); 

void em_ble_disable_latency(uint16_t conn_handle, uint32_t seconds );

void em_ble_enable_latency(uint16_t conn_handle);

bool iqw_app_ble_query_is_connected(void);

#endif 

